/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import db.mysql.SQLClient;
import libCore.config.Config;

/**
 *
 * @author KieuAnh
 */
public class DBContext
{
    private DBContext() {}
    
    static
    {
        //initRedis();
        REDIS = new JedisAdapter(JedisPoolFactory.createFromConfig("redis"));
        REDIS2 = new JedisAdapter(JedisPoolFactory.createFromConfig("redis2"));
        SQL = new SQLClient();
    }
    
//    private static void initRedis()
//    {
//        REDIS_MODE = RedisMode.valueOf(Config.getParam("redis-mode", "mode"));
//        if (REDIS_MODE == RedisMode.single)
//        {
//            REDIS = initSingleRedis();
//            MREDIS = null;
//        }
//        else
//        {
//            REDIS = null;
//            MREDIS = new SwitchableMultiRedis(new MultiRedisImpl());
//        }
//    }
    
    private static Redis initSingleRedis()
    {
        String host = Config.getParam("redis", "host");
        int port = Integer.valueOf(Config.getParam("redis", "port"));
        String password = Config.getParam("redis", "pass");
        int db = Integer.valueOf(Config.getParam("redis", "database"));
        
        int maxActive = Integer.valueOf(Config.getParam("redis", "max_active"));
        int maxIdle = Integer.valueOf(Config.getParam("redis", "max_idle"));
        int maxWait = Integer.valueOf(Config.getParam("redis", "max_wait"));
        int timeOut = Integer.valueOf(Config.getParam("redis", "timeout"));
        
        return new JedisAdapter(JedisPoolFactory.create(host, port,
                timeOut, password, db, maxActive, maxIdle, maxWait));
    }
    
    private static enum RedisMode
    {
        single,
        multi
    }
//    private static RedisMode REDIS_MODE;
    private final static Redis REDIS;
    private final static Redis REDIS2;
//    private static SwitchableMultiRedis MREDIS;
    private static final SQLClient SQL;
    
    public static Redis Redis() {
        return REDIS;
    }
    
    public static Redis Redis2()
    {
        return REDIS2;
    }
    
//    static void changeRedis(int index)
//    {
//        MREDIS.changeDefault(index);
//    }
//    
//    public static void switchRedis(int index, Runnable runner)
//    {
//        Integer oldRedis = MREDIS.getCurrentDefault();
//        MREDIS.changeDefault(index);
//        runner.run();
//        MREDIS.changeDefault(oldRedis);
//    }
    
    public static SQLClient SQL()
    {
        return SQL;
    }
}
